﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace CodeRepositoryConsole.FCL
{
    /// <summary>
    /// <seealso cref="https://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application"/>
    /// <seealso cref="https://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path"/>
    /// <seealso cref="https://blogs.msdn.microsoft.com/suzcook/2003/06/26/assembly-codebase-vs-assembly-location/"/>
    /// </summary>
    class FileSystemPath
    {
        // If work with System.Windows.Forms you can also use 
        // Application.ExecutablePath or Application.StartupPath

        public void GetByAssembly()
        {
            /**
             * System.Reflection.Assembly.GetExecutingAssembly().Location
             * Combine that with System.IO.Path.GetDirectoryName if all you want is the directory.
            */

            // To get the location the assembly normally resides on disk or the install directory
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;

            Console.WriteLine("Executing CodeBase:" + codeBase);
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            Console.WriteLine(System.IO.Path.GetDirectoryName(path));

            // System.Reflection.Assembly.GetExecutingAssembly().Location returns where 
            // the executing assembly is currently located, which may or may not be where the assembly is located when not executing.
            // In the case of shadow copying assemblies, you will get a path in a temp directory.
            // System.Reflection.Assembly.GetExecutingAssembly().CodeBase will return the 'permenant' path of the assembly.
            string location = Assembly.GetExecutingAssembly().Location;
            Console.WriteLine("Executing Location:" + location);
            Console.WriteLine(System.IO.Path.GetDirectoryName(location));

            //-------------------------- Use GetEntryAssembly  -------------------------------------- //

            codeBase = Assembly.GetEntryAssembly().CodeBase;
            Console.WriteLine("Entry CodeBase:" + codeBase);
            uri = new UriBuilder(codeBase);
            path = Uri.UnescapeDataString(uri.Path);
            Console.WriteLine(System.IO.Path.GetDirectoryName(path));


            location = Assembly.GetEntryAssembly().Location;
            Console.WriteLine("Entry Location:" + location);
            Console.WriteLine(System.IO.Path.GetDirectoryName(location));

            //------------------------- Use Environment.CurrentDirectory --------------------------- //

            // Current Directory is different from the directory containing the current executable. 
            // Environment.CurrentDirectory is a value that can and will change through the course 
            // of running your application. For instance, using default parameters, the OpenFileDialog 
            // in WinForms will change this value to the directory where the file was selected from
            // <see ref="https://stackoverflow.com/questions/10993721/how-to-get-execution-directory-of-console-application">

            // This simply calls Directory.GetCurrentDirectory()

            codeBase = Environment.CurrentDirectory;

            //------------------------ Use Directory.GetCurrentDirectory() ------------------------- //
            // This does an interop call using the winapi GetCurrentDirectory call inside kernel32.dll, 
            // which means the launching process’ folder will often be returned.Also as the MSDN documents say, 
            // it’s not guaranteed to work on mobile devices.
            codeBase = System.IO.Directory.GetCurrentDirectory();

        }


        public void GetByAppDomain()
        {
            /*
            * In an ASP.NET application, this will be the application root directory, not the bin subfolder - which is probably what you usually want. 
            * In a client application, it will be the directory containing the main executable.
            */
            Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
        }
    }
}
